package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.xca.znb.R;
import com.xca.znb.adapter.GoodsHomeAdapter;
import com.xca.znb.contact.MainHomeContact;
import com.xca.znb.entity.Goods;
import com.xca.znb.model.impl.HomeImpl;
import com.xca.znb.presenter.HomePresenter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class HomeFragment extends Fragment implements View.OnClickListener , MainHomeContact {
    private GridView HomeItemsShow ;
    private View home_fragment ;
    private ImageButton ToSearch , ToSeal ;
//    private TabLayout HomeTab ;
    private TextView HomeSuggest , HomeFruit , HomeRFGO , HomeTools ;


    private GoodsHomeAdapter adapter;
    private List<Goods> goodsList;
    private HomePresenter presenter ;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (home_fragment == null){
            home_fragment = inflater.inflate(R.layout.fragment1 , container ,false);
        }
        ToSeal = home_fragment.findViewById(R.id.toSaleBtn);
        ToSearch = home_fragment.findViewById(R.id.toSearchBtn);
        HomeSuggest = home_fragment.findViewById(R.id.home_suggest);
        HomeFruit = home_fragment.findViewById(R.id.home_fruit);
        HomeRFGO = home_fragment.findViewById(R.id.home_RFGO);
        HomeTools = home_fragment.findViewById(R.id.home_tools);
        HomeItemsShow = home_fragment.findViewById(R.id.home_items_show);

        HomeImpl home = new HomeImpl(getActivity());
        presenter = new HomePresenter(home);

            ToSearch.setOnClickListener(this);
            onToSaleButtonClick();
        onHomeSuggestTextClick();
        onHomeFruitTextClick();
        onHomeRFGOTextClick();
        onHomeToolsTextClick();

        HomeSuggest.setSelected(true);
            goodsList = presenter.getAllGoods();
            adapter = new GoodsHomeAdapter(getActivity() , goodsList);
            HomeItemsShow.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        HomeItemsShow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView showId = view.findViewById(R.id.home_item_id) ;
                String sId = showId.getText().toString() ;
                int goodsId = Integer.parseInt(sId);
                SharedPreferences sharedPreferences = getActivity().getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
                String phone = sharedPreferences.getString("phone" , "");
                LocalDateTime nowTime = LocalDateTime.now();
                String secTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                presenter.addOrUpdateFoot(goodsId,phone,secTime);
                Intent intent = new Intent(getActivity() , GoodsDetailActivity.class) ;
                intent.putExtra("goodsId" , sId) ;
                startActivity(intent);
            }
        });

        return home_fragment ;
    }

    @Override
    public void showGoodsData(List<Goods> data) {
        // This method can be used to update the UI with new data
    }

    @Override
    public void showError() {
        // This method can be used to show error messages
    }

    public void onResume(){
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.home_suggest){
            HomeSuggest.setSelected(true);
            HomeFruit.setSelected(false);
            HomeRFGO.setSelected(false);
            HomeTools.setSelected(false);

            goodsList = presenter.getAllGoods();
            adapter = new GoodsHomeAdapter(getActivity() , goodsList);
            HomeItemsShow.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } else if (v.getId()==R.id.home_fruit) {
            HomeSuggest.setSelected(false);
            HomeFruit.setSelected(true);
            HomeRFGO.setSelected(false);
            HomeTools.setSelected(false);

            String type = HomeFruit.getText().toString();
            goodsList = presenter.getGoodsByType(type);
            adapter = new GoodsHomeAdapter(getActivity() , goodsList);
            HomeItemsShow.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } else if (v.getId()==R.id.home_RFGO) {
            HomeSuggest.setSelected(false);
            HomeFruit.setSelected(false);
            HomeRFGO.setSelected(true);
            HomeTools.setSelected(false);

            String type = HomeRFGO.getText().toString();
            goodsList = presenter.getGoodsByType(type);
            adapter = new GoodsHomeAdapter(getActivity() , goodsList);
            HomeItemsShow.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } else if (v.getId()==R.id.home_tools) {
            HomeSuggest.setSelected(false);
            HomeFruit.setSelected(false);
            HomeRFGO.setSelected(false);
            HomeTools.setSelected(true);

            String type = HomeTools.getText().toString();
            goodsList = presenter.getGoodsByType(type);
            adapter = new GoodsHomeAdapter(getActivity() , goodsList);
            HomeItemsShow.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } else if (v.getId()==R.id.toSaleBtn) {
            Intent intent = new Intent(getActivity() , SaleMain.class);
            startActivity(intent);
            getActivity().finish();
        } else if (v.getId()==R.id.toSearchBtn) {
            Intent intent = new Intent(getActivity() , SearchActivity.class) ;
            startActivity(intent);
        }
    }

    @Override
    public void onToSearchButtonClick() {

    }

    @Override
    public void onToSaleButtonClick() {
        ToSeal.setOnClickListener(this);
    }

    @Override
    public void onHomeSuggestTextClick() {
        HomeSuggest.setOnClickListener(this);
    }

    @Override
    public void onHomeFruitTextClick() {
        HomeFruit.setOnClickListener(this);
    }

    @Override
    public void onHomeRFGOTextClick() {
        HomeRFGO.setOnClickListener(this);
    }

    @Override
    public void onHomeToolsTextClick() {
        HomeTools.setOnClickListener(this);
    }

}
